﻿#include "ItemRentalDialog.h"
#include "../../MirGraphics/MLibrary.h"
#include "../../Settings.h"
#include "../../MirControls/MirButton.h"
#include "../../MirSounds/SoundManager.h"
#include "../../Forms/Client.CMain.h"
#include "../../MirNetwork/Network.h"
#include "../../../Shared/ClientPackets.h"

using namespace Client::MirControls;
using namespace Client::MirGraphics;
using namespace Client::MirSounds;
using namespace Client::MirNetwork;
namespace C = ClientPackets;

namespace Client::MirScenes::Dialogs
{

    ItemRentalDialog::ItemRentalDialog()
    {
        setIndex(1);
        setLibrary(Libraries::Prguse3);
        setMovable(true);
        Size tempVar(400, 174);
        setSize(&tempVar);
        Point tempVar2((Settings::ScreenWidth - getSize()->Width) / 2, (Settings::ScreenHeight - getSize()->Height) / 2);
        setLocation(&tempVar2);
        setSort(true);

        // Title

        auto windowTitle = new MirImageControl();
        windowTitle->setIndex(0);
        windowTitle->setLibrary(Libraries::Prguse3);
        Point tempVar3(22, 8);
        windowTitle->setLocation(&tempVar3);
        windowTitle->setParent(this);

        // Rented Tab

        auto rentedTabButton = new MirButton();
        rentedTabButton->setIndex(2);
        rentedTabButton->setHoverIndex(2);
        Point tempVar4(8, 32);
        rentedTabButton->setLocation(&tempVar4);
        Size tempVar5(72, 23);
        rentedTabButton->setSize(&tempVar5);
        rentedTabButton->setLibrary(Libraries::Prguse3);
        rentedTabButton->setParent(this);
        rentedTabButton->setPressedIndex(2);
        rentedTabButton->setSound(SoundList::ButtonA);
        rentedTabButton->setEnabled(false);

        // Borrowed Tab

        auto borrowedTabButton = new MirButton();
        borrowedTabButton->setIndex(3);
        Point tempVar6(81, 32);
        borrowedTabButton->setLocation(&tempVar6);
        Size tempVar7(84, 23);
        borrowedTabButton->setSize(&tempVar7);
        borrowedTabButton->setLibrary(Libraries::Prguse3);
        borrowedTabButton->setParent(this);
        borrowedTabButton->setSound(SoundList::ButtonA);
        borrowedTabButton->setEnabled(false);

        // Rent Item Button

        auto rentItemButton = new MirButton();
        rentItemButton->setIndex(4);
        rentItemButton->setHoverIndex(5);
        Point tempVar8(295, 144);
        rentItemButton->setLocation(&tempVar8);
        Size tempVar9(85, 29);
        rentItemButton->setSize(&tempVar9);
        rentItemButton->setLibrary(Libraries::Prguse3);
        rentItemButton->setParent(this);
        rentItemButton->setPressedIndex(6);
        rentItemButton->setSound(SoundList::ButtonA);
        rentItemButton->Click += [&] (o, e)
        {
            C::ItemRentalRequest tempVar10();
            Network::Enqueue(&tempVar10);
        };

        // Close Button

        auto closeButton = new MirButton();
        closeButton->setHoverIndex(361);
        closeButton->setIndex(360);
        Point tempVar11(375, 3);
        closeButton->setLocation(&tempVar11);
        closeButton->setLibrary(Libraries::Prguse2);
        closeButton->setParent(this);
        closeButton->setPressedIndex(362);
        closeButton->setSound(SoundList::ButtonA);
        closeButton->Click += [&] (o, e)
        {
            Toggle();
        };

        // Item Rows

        for (auto i = 0; i < _itemRows.size(); i++)
        {
            ItemRow *tempVar12 = new ItemRow();
            tempVar12->setParent(this);
            Point tempVar13(0, 78 + i * 21);
            tempVar12->setLocation(&tempVar13);
            Size tempVar14(383, 21);
            tempVar12->setSize(&tempVar14);
            _itemRows[i] = tempVar12;

            delete tempVar12;
        }

        delete closeButton;
        delete rentItemButton;
        delete borrowedTabButton;
        delete rentedTabButton;
        delete windowTitle;
    }

    void ItemRentalDialog::Toggle()
    {
        setVisible(!getVisible());

        if (getVisible())
        {
            RequestRentedItems();
        }
    }

    void ItemRentalDialog::ReceiveRentedItems(std::vector<ItemRentalInformation*> &rentedItems)
    {
        for (auto i = 0; i < _itemRows.size(); i++)
        {
            _itemRows[i]->Clear();

            if (i < rentedItems.size() && rentedItems[i] != nullptr)
            {
//C# TO C++ CONVERTER TODO TASK: There is no C++ equivalent to 'ToString':
                _itemRows[i]->Update(rentedItems[i]->ItemName, rentedItems[i]->RentingPlayerName, rentedItems[i]->ItemReturnDate.ToString(CultureInfo::InvariantCulture));
            }
        }
    }

    void ItemRentalDialog::RequestRentedItems()
    {
        if (_lastRequestTime > CMain::getNow())
        {
            return;
        }

        _lastRequestTime = CMain::getNow().AddSeconds(60);
        C::GetRentedItems tempVar();
        Network::Enqueue(&tempVar);
    }

    ItemRentalDialog::ItemRow::ItemRow() : _itemNameLabel(new MirLabel();
            Size tempVar(128, 20);
            _itemNameLabel->setSize(&tempVar);
            Point tempVar2(5, 0);
            _itemNameLabel->setLocation(&tempVar2);
            _itemNameLabel->setDrawFormat(TextFormatFlags::HorizontalCenter);
            _itemNameLabel->setParent(this);
            _itemNameLabel->setNotControl(true)), _rentingPlayerLabel(new MirLabel();
            Size tempVar3(128, 20);
            _rentingPlayerLabel->setSize(&tempVar3);
            Point tempVar4(137, 0);
            _rentingPlayerLabel->setLocation(&tempVar4);
            _rentingPlayerLabel->setDrawFormat(TextFormatFlags::HorizontalCenter);
            _rentingPlayerLabel->setParent(this);
            _rentingPlayerLabel->setNotControl(true)), _returnDateLabel(new MirLabel();
            Size tempVar5(128, 20);
            _returnDateLabel->setSize(&tempVar5);
            Point tempVar6(264, 0);
            _returnDateLabel->setLocation(&tempVar6);
            _returnDateLabel->setDrawFormat(TextFormatFlags::HorizontalCenter);
            _returnDateLabel->setParent(this);
            _returnDateLabel->setNotControl(true))
            {


            }

    void ItemRentalDialog::ItemRow::Clear()
    {
        setVisible(false);

        _itemNameLabel->setText("");
        _rentingPlayerLabel->setText("");
        _returnDateLabel->setText("");
    }

    void ItemRentalDialog::ItemRow::Update(const std::string &itemName, const std::string &rentingPlayerName, const std::string &returnDate)
    {
        _itemNameLabel->setText(itemName);
        _rentingPlayerLabel->setText(rentingPlayerName);
        _returnDateLabel->setText(returnDate);

        setVisible(true);
    }
}
